#_*_coding:utf-8_*_
#@author:侯松林

"""
使用棋盘格及自选风景图像，分别使用SIFT、FAST及ORB算子检测角点，并比较分析检测结果
"""
import cv2
import copy
import numpy as np
from matplotlib import pyplot as plt
# 加载图片并显示
#棋盘图片
# img = cv2.imread('chessboard.png')
#风景图片
img = cv2.imread('fengjing.jpg')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('img_gray',img_gray)

#SIFT
img1=copy.deepcopy(img_gray)
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(img1,None)
img_sift = cv2.drawKeypoints(img1, kp, None, color=(255,0,0))
cv2.imshow('img_sift',img_sift)


#FAST检测角点
img2=copy.deepcopy(img_gray)
fast = cv2.FastFeatureDetector_create()
kp = fast.detect(img2,None)
img_fast = cv2.drawKeypoints(img2, kp, None, color=(255,0,0))
cv2.imshow('fast',img_fast)

#ORB检测角点
img3=copy.deepcopy(img_gray)
orb = cv2.ORB_create()
kp = orb.detect(img3,None)
img_orb = cv2.drawKeypoints(img3, kp, None, color=(255,0,0))
cv2.imshow('orb',img_orb)
"""
结论：
棋盘图：SIFT有很多中心的点(估计跟算法向下采用有关)，ORB整体检测效果还可以，FAST点很少(跟算法有关，有棋盘格刚好是对称的)
风景画：SIFT点效果最好，ORB部分关键点都没有描出来，FAST点太多太乱了，很多都没有进行抑制
"""



#Harris角点检测
#棋盘图片
img = cv2.imread('chessboard.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst_block9_ksize19 = cv2.cornerHarris(gray, 9, 19, 0.04)
img1 = np.copy(img)
img1[dst_block9_ksize19 > 0.01 * dst_block9_ksize19.max()] = [0, 0, 255]

dst_block5_ksize19 = cv2.cornerHarris(gray, 5, 19, 0.04)
img2 = np.copy(img)
img2[dst_block5_ksize19 > 0.01 * dst_block5_ksize19.max()] = [0, 0, 255]

dst_block9_ksize5 = cv2.cornerHarris(gray, 9, 5, 0.04)
img3 = np.copy(img)
img3[dst_block9_ksize5 > 0.01 * dst_block9_ksize5.max()] = [0, 0, 255]

dst_block9_ksize31 = cv2.cornerHarris(gray, 9, 31, 0.04)
img4 = np.copy(img)
img4[dst_block9_ksize31 > 0.01 * dst_block9_ksize31.max()] = [0, 0, 255]

dst_block9_ksize19_k6 = cv2.cornerHarris(gray, 9, 19, 0.06)
img5 = np.copy(img)
img5[dst_block9_ksize19_k6 > 0.01 * dst_block9_ksize19_k6.max()] = [0, 0, 255]

dst_block9_ksize19_k6_1e_5 = cv2.cornerHarris(gray, 9, 19, 0.06)
img6 = np.copy(img)
img6[dst_block9_ksize19_k6_1e_5 > 0.00001 * dst_block9_ksize19_k6_1e_5.max()] = [0, 0, 255]

titles = ["Original", "block9_ksize19", "dst_block5_ksize19", "dst_block9_ksize5", "dst_block9_ksize31",
          "dst_block9_ksize19_k6", "dst_block9_ksize19_k6_1e_5"]
imgs = [img, img1, img2, img3, img4, img5, img6]
for i in range(len(titles)):
    plt.subplot(3, 3, i + 1), plt.imshow(imgs[i]), plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()
"""
结论：
棋盘图：harris检测棋盘格很准确
"""

cv2.waitKey()
cv2.destroyAllWindows()

